package 深度优先搜索;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class Solution257二叉树所有路径 {
    List<String> result=new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if(root==null){
            return result;
        }
        dfs("",root);
        return result;
    }

    private void dfs(String path,TreeNode node){
        if(node==null){
            return;
        }
        path+=node.val;
        if(node.right!=null){
            dfs(path+"->",node.right);
        }
        if(node.left!=null){
            dfs(path+"->",node.left);
        }
        if(node.right==null&&node.left==null){
            result.add(path);
        }
    }

    @Test
    public void test(){
        TreeNode node=new TreeNode(1);
        TreeNode node2=new TreeNode(2);
        TreeNode node3=new TreeNode(3);
        node.left=node2;
        node.right=node3;
        TreeNode node4=new TreeNode(5);
        node2.right=node4;
        System.out.println(binaryTreePaths(node));
    }
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

}
